package net.ecpt.dubbo.clm;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;

public class ClmUtils {
    public static String module = ClmUtils.class.getName();
    public static final String OFBIZ_HOME = System.getProperty("ofbiz.home");

    public static Map<String, Object> optimizingResults(Map<String, Object> serviceResults, String entityName) {
	Delegator delegator = DelegatorFactory.getDelegator("default");
	ModelEntity me = delegator.getModelEntity(entityName);
	try {
	    Iterator<ModelField> it = me.getFieldsIterator();
	    while (it.hasNext()) {
		ModelField pkf = it.next();
		Object pkValue = serviceResults.get(pkf.getName());
		if (UtilValidate.isEmpty(pkValue)) {
		    pkValue = serviceResults.get(pkf.getName());
		    serviceResults.put(pkf.getName(), pkValue);
		}
		if (!pkf.getName().equals(pkf.getName())) {
		    serviceResults.remove(pkf.getName());
		}
		String type = pkf.getType();
		if (UtilValidate.isNotEmpty(pkValue)) {
		    if (type.equals("date-time")) {
			if (!isValidDate(String.valueOf(pkValue))) {
			    pkValue = pkValue + " 00:00:00";
			}
			serviceResults.put(pkf.getName(), Timestamp.valueOf(String.valueOf(pkValue)));
		    } else if (type.equals("date") || type.equals("Date")) {
			serviceResults.put(pkf.getName(),
				java.sql.Date.valueOf(String.valueOf(serviceResults.get(pkf.getName()))));
		    } else if (type.equals("time")) {
			serviceResults.put(pkf.getName(),
				java.sql.Time.valueOf(String.valueOf(serviceResults.get(pkf.getName()))));
		    } else if (type.equals("currency-amount") || type.equals("currency-precise")
			    || type.equals("fixed-point")) {
			serviceResults.put(pkf.getName(),
				BigDecimal.valueOf(Double.valueOf(String.valueOf(serviceResults.get(pkf.getName())))));
		    } else if (type.equals("floating-point")) {
			serviceResults.put(pkf.getName(),
				Double.valueOf(String.valueOf(serviceResults.get(pkf.getName()))));
		    } else if (type.equals("numeric")) {
			serviceResults.put(pkf.getName(),
				Long.valueOf(String.valueOf(serviceResults.get(pkf.getName()))));
		    } else if (type.equals("byte-array")) {
			byte[] o = String.valueOf(serviceResults.get(pkf.getName())).getBytes();
			serviceResults.put(pkf.getName(), o);
		    } else {
			serviceResults.put(pkf.getName(), String.valueOf(serviceResults.get(pkf.getName())));
		    }
		}
	    }
	} catch (NumberFormatException e) {
	    Debug.logError(e.getMessage(), module);
	}

	return serviceResults;
    }

    public static boolean isValidDate(String str) {
	boolean convertSuccess = true;
	// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	try {
	    // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
	    format.setLenient(false);
	    format.parse(str);
	} catch (ParseException e) {
	    convertSuccess = false;
	}
	return convertSuccess;
    }

}